package com.techie.mdm.appbar.showcase;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.techie.mdm.appbar.showcase.binding.BeansBindingValueModelAdapter;
import com.techie.mdm.appbar.showcase.binding.DynamicFormatString;
import jdesktop.appbar.Edge;
import jdesktop.appbar.Sides;
import org.springframework.binding.form.FieldMetadata;
import org.springframework.binding.form.support.ReadOnlyFieldMetadata;
import org.springframework.binding.value.ValueModel;
import org.springframework.binding.value.support.ValueHolder;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.FormModelHelper;
import org.springframework.richclient.form.binding.Binding;
import org.springframework.richclient.form.binding.swing.EnumRadioButtonBinder;
import org.springframework.richclient.form.builder.TableFormBuilder;
import jdesktop.appbar.win32.WAppBar;

import javax.swing.*;
import java.util.Collections;
import com.jgoodies.forms.layout.*;

/**
 * Appbar model exists without an actual window.
 * User clicks createWindow to create a window.
 * registered property controls if window is registered or not.
 * Window can be disposed.
 *
 *
 */
public class AppBarForm extends AbstractForm
{
    private AppBarBean model;

    public AppBarForm(AppBarBean model)
    {
        super(FormModelHelper.createFormModel(model, false));
        this.model = model;
        setId("appBarForm");
    }

    @Override
    protected JComponent createFormControl()
    {
        TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
        builder.setLabelAttributes("colGrId=label colSpec=right:pref");

        builder.addSeparator("Location");
        builder.row();

        addEdgeSelection(builder);
        builder.row();

        builder.add("appBar."+ WAppBar.DESIREDBARSIZE);
        builder.add("appBar."+ WAppBar.BARSIZE);
//        fb.add(AppBar.ALLOWEDEDGES);
        builder.row();

        builder.add("appBar."+ WAppBar.DOCKED);
        builder.row();

        builder.addSeparator("Window Bounds");
        builder.row();

        String hasWindow = AppBarBean.APPBARWINDOW+"."+AppBarWindow.HASWINDOW;
        builder.add(hasWindow);
        builder.row();

        String registered = AppBarBean.APPBARWINDOW+"."+ WAppBar.REGISTERED;
        builder.add(registered);
/*        FieldMetadata registeredMeta = getFormModel().getFieldMetadata(registered);
        Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ,
                model.getAppBarWindow(), BeanProperty.create("hasWindow"),
                registeredMeta, BeanProperty.create("enabled")).bind();
*/                
        builder.row();

        String appBarBounds = createRectProp(AppBarBean.APPBARWINDOW+"."+AppBarWindow.APPBARBOUNDS);
        builder.add(appBarBounds);

/*
        fc.add("windowBounds.x");
        fc.add("windowBounds.y");
        fc.row();
        fc.add("windowBounds.widh");
        fc.add("windowBounds.height");
  */
        return builder.getForm();
    }

    /**
     * Adds edge selection and allowed edges.
     * @param builder
     */
    private JPanel addEdgeSelection(TableFormBuilder builder)
    {
        JPanel edgeSelection = new JPanel();
        EnumRadioButtonBinder edgeSelectionBinder = new EnumRadioButtonBinder();
        Binding edgeBinding = edgeSelectionBinder.bind(edgeSelection, getFormModel(), "appBar.edge", Collections.emptyMap());
        builder.add(edgeBinding);
        
        FormLayout layout = (FormLayout) edgeSelection.getLayout();
        layout.appendColumn(FormFactory.RELATED_GAP_COLSPEC);
        layout.appendColumn(FormFactory.DEFAULT_COLSPEC);

        Edge[] edges = Edge.class.getEnumConstants();
        for (int i=0; i<edges.length; i++)
        {
            JCheckBox box = new JCheckBox();
            edgeSelection.add(box, new CellConstraints(3, i*2+1));
            Edge edge = edges[i];
            // yuk - I hate all this string manipulation for property access.
            
        }

        return edgeSelection;
    }

    /**
     * Creates a new property that provides a string representation of a rectangle.
     * @param propertyPath  The propertyPath of the rectangle
     * @return The new property name.
     */
    protected String createRectProp(String propertyPath)
    {
        // todo - N/A from message source
        ValueModel source = getValueModel(propertyPath);
        ValueModel[] arguments = BeansBindingValueModelAdapter.buildValueModels(source,  "x", "y", "width", "height", "${x+width}", "${y+height}");

        ValueModel result = DynamicFormatString.createModel(arguments,
                new ValueHolder(getMessage("notAvailable")), new ValueHolder("[{0},{1}]-[{4},{5}] ({2}x{3})"));

        FieldMetadata fieldMetaData = new ReadOnlyFieldMetadata(getFormModel(), String.class);
        String newProp = propertyPath+".text";
        getFormModel().add(newProp, result, fieldMetaData);
        return newProp;
    }
}
